package com.hengtuo.kaiyuanzhongguo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtuo.util.AcquisitionTime;
import com.hengtuo.util.ExceptionApp;
import com.hengtuo.util.ShakeListener;
import com.hengtuo.vo.Shark;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.thoughtworks.xstream.XStream;

import java.util.List;

/**
 * 摇一摇     http://www.oschina.net/action/api/rock_rock
 */
public class SharkActivity extends Activity implements View.OnClickListener {

    private ImageView sharkback;
    ShakeListener mShakeListener = null;
    TranslateAnimation animation_translate;
    private ImageView iv;
    private HttpUtils utils;
    private BitmapUtils bitmap;
    private ImageView head;
    private TextView title;
    private TextView detail;
    private TextView author;
    private TextView uptime;
    private TextView count;
    private RelativeLayout relativie;
    private String url;
    private ProgressBar progressBar;

    ProgressDialog progressDialog = null;
    public static final int MESSAGETYPE = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shark);

        bitmap = new BitmapUtils(SharkActivity.this);

        initialize();

        mShakeListener = new ShakeListener(this);
        mShakeListener.setOnShakeListener(new shakeLitener());


    }

    private class shakeLitener implements ShakeListener.OnShakeListener {

        @Override
        public void onShake() {

            ShakeListener.Vibrate(SharkActivity.this, 500);
            animation_translate = new TranslateAnimation(1f, 250f, 0f, 0f);
            animation_translate.setInterpolator(new AccelerateInterpolator());
            animation_translate.setRepeatMode(Animation.REVERSE);
            animation_translate.setRepeatCount(1);//设置动画执行多少次，如果是-1的话就是一直重复
            animation_translate.setDuration(500);//设置时间持续时间为 5000毫秒
            iv.startAnimation(animation_translate);

            progressBar.setVisibility(View.VISIBLE);
            // 网络请求
            getHttp();

        }

    }

    private void getHttp() {
        ExceptionApp.log("tag", "网络请求");
        utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, "http://www.oschina.net/action/api/rock_rock", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String str = responseInfo.result;
                ExceptionApp.log("tag", str);
                XStream x = new XStream();
                x.processAnnotations(Shark.class);
                Shark shark = new Shark();
                shark = (Shark) x.fromXML(str);
                // 赋值
                url = shark.getUrl();
                bitmap.display(head, shark.getImage());
                title.setText(shark.getTitle());
                detail.setText(shark.getDetail());
                author.setText(shark.getAuthor());

                String user_time = AcquisitionTime.dateToTimestamp(shark.getPubDate());
                String time = AcquisitionTime.getStandardDate(user_time);
                uptime.setText(time);

                count.setText("" + shark.getCommentCount());
                progressBar.setVisibility(View.GONE);
                relativie.setVisibility(View.VISIBLE);

                // 详情界面
                relativie.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent it = new Intent(SharkActivity.this, SharkDetailActivity.class);
                        it.putExtra("url", url);
                        startActivity(it);
                    }
                });
            }

            @Override
            public void onFailure(HttpException e, String s) {
                ExceptionApp.log("tag", "网络请求失败");
            }
        });

    }

    private void initialize() {

        sharkback = (ImageView) findViewById(R.id.shark_back);
        sharkback.setOnClickListener(this);
        iv = (ImageView) findViewById(R.id.shark_imageView1);
        head = (ImageView) findViewById(R.id.shark_head);
        title = (TextView) findViewById(R.id.shark_title);
        detail = (TextView) findViewById(R.id.shark_detail);
        author = (TextView) findViewById(R.id.shark_authorid);
        uptime = (TextView) findViewById(R.id.shark_update);
        count = (TextView) findViewById(R.id.shark_count);
        relativie = (RelativeLayout) findViewById(R.id.shark_relativie);
        progressBar = (ProgressBar) findViewById(R.id.shark_cicle_progressbar);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.shark_back:
                finish();
                break;

        }
    }


}
